<?php

/**
 * This is the model class for table "project_profile".
 *
 * The followings are the available columns in table 'project_profile':
 * @property string $id
 * @property string $project_id
 * @property string $parent_project
 * @property string $name
 * @property integer $hierarchal_type
 * @property string $about
 * @property string $requirement
 * @property integer $status
 * @property string $author_id
 * @property string $supervisor_id
 * @property string $vote
 * @property double $rate
 * @property string $logo
 * @property string $create_time
 * @property string $last_update
 *
 * The followings are the available model relations:
 * @property User $supervisor
 * @property Project $project
 * @property User $author
 */
class ProjectProfile extends CActiveRecord
{
  
         const STATUS_DRAFT=1;
         const STATUS_PENDING_APPROVAL=2;
         const STATUS_UNASSIGNED=3;
         const STATUS_INPROGRESS=4;
         const STATUS_COMPLETED=5;
	/**
	 * Returns the static model of the specified AR class.
	 * @return ProjectProfile the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'project_profile';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_project, name, hierarchal_type, about, requirement, status, supervisor_id', 'required'),
			array('hierarchal_type, status', 'numerical', 'integerOnly'=>true),
			array('rate', 'numerical'),
			array('parent_project, supervisor_id', 'length', 'max'=>10),
			array('name', 'length', 'max'=>40),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, project_id, parent_project, name, hierarchal_type, about, requirement, status, author_id, supervisor_id, vote, rate, create_time, last_update', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'supervisor' => array(self::BELONGS_TO, 'User', 'supervisor_id'),
			'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
			'author' => array(self::BELONGS_TO, 'User', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'project_id' => 'Project',
			'parent_project' => 'Parent Project',
			'name' => 'Name',
			'hierarchal_type' => 'Hierarchal Type',
			'about' => 'About',
			'requirement' => 'Requirement',
			'status' => 'Status',
			'author_id' => 'Author',
			'supervisor_id' => 'Supervisor',
			'vote' => 'Vote',
			'rate' => 'Rate',
			'logo' => 'Logo',
			'create_time' => 'Created Time',
			'last_update' => 'Updated Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('project_id',$this->project_id,true);
		$criteria->compare('parent_project',$this->parent_project,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('hierarchal_type',$this->hierarchal_type);
		$criteria->compare('about',$this->about,true);
		$criteria->compare('requirement',$this->requirement,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('author_id',$this->author_id,true);
		$criteria->compare('supervisor_id',$this->supervisor_id,true);
		$criteria->compare('vote',$this->vote,true);
		$criteria->compare('rate',$this->rate);
		$criteria->compare('logo',$this->logo,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('last_update',$this->last_update,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getUrl()
        {
            return Yii::app()->createUrl('projectProfile/view', array(
            'id'=>$this->id,
            'name'=>$this->name,
            ));
        }

        protected function beforeSave()
        {
                if(parent::beforeSave())
                {
                    if($this->isNewRecord)
                    {
                        $this->create_time=$this->last_update=new CDbExpression('NOW()');
                        $this->author_id=Yii::app()->user->id;
                        $this->project_id = '1';
                    }
                    else
                        $this->last_update=new CDbExpression('NOW()');

                    return true;
                }
                else
                    return false;
        }
}